package com.xyrl.project.manager.common;

import com.xyrl.common.core.controller.BaseController;
import com.xyrl.project.common.base.vo.ResponseData;
import com.xyrl.project.common.common.entity.Attachment;
import com.xyrl.project.common.common.service.AttachmentService;
import com.xyrl.project.common.common.vo.AttachmentVo;
import com.xyrl.project.manager.util.ShiroUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;

/**
 * 附件上传工具类（阿里oss）
 *
 * @author zhongp
 * @version 1.0
 * @className OssController
 * @company 星纭融链科技服务有限公司
 * @date 2019-12-27 09:44:28
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("alioss")
@Api(value = "附件上传", tags = "公共模块")
public class AliOssController extends BaseController {
    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private AttachmentService attachmentService;


    /**
     * @param file 要上传的文件
     * @return
     */
    @PostMapping("fileUpload")
    public ResponseData upload(@RequestParam("fileName") MultipartFile file, @NotBlank(message = "文件模块不能为空") String module) {
        log.info("文件上传模块:{}", module);
        try{
            AttachmentVo attachment = attachmentService.addAttachment(file, ShiroUtil.loginUserId(), ShiroUtil.loginUser().getUserType(), module);
            return ResponseData.success(attachment);
        }catch (Exception e){
            return ResponseData.error(e.getMessage());
        }
    }

//    /**
//     * 显示单张图片
//     *
//     * @return
//     */
//    @RequestMapping("show")
//    public ResponseEntity showPhotos(String fileName) {
//
//        try {
//            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
//            return ResponseEntity.ok(resourceLoader.getResource("file:" + fileName));
//        } catch (Exception e) {
//            return ResponseEntity.notFound().build();
//        }
//    }

}
